package com.nondo.rdp.finance.service.impl.activity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.rdp.auth.domain.Role;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.finance.domain.activity.MarketCourse;
import com.nondo.rdp.finance.repository.activity.MarketCourseRepository;
import com.nondo.rdp.finance.service.activity.MarketCourseService;

@Transactional
@Service
public class MarketCourseServiceImpl implements MarketCourseService {
    @Autowired
    MarketCourseRepository marketCourseRepository;

    @Override
    public MarketCourse findOne(Long id) {
        return this.marketCourseRepository.findOne(id);
    }

    @Override
    public MarketCourse save(MarketCourse MarketCourse) {
        return this.marketCourseRepository.save(MarketCourse);
    }

    @Override
    public void update(MarketCourse MarketCourse) {
        this.marketCourseRepository.save(MarketCourse);
    }

    @Override
    public void delete(Long id) {
        this.marketCourseRepository.deleteById(id);
    }

    @Override
    public Page<MarketCourse> findAll(Page<MarketCourse> page, MarketCourse marketCourse) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<MarketCourse> dictPage = marketCourseRepository.findAll((root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();
            
			if (marketCourse != null) {
	            //搜索ID
	            if (marketCourse.getId() != null && marketCourse.getId() != 0) {
	            	predicates.add(builder.equal(root.get("id"), marketCourse.getId()));
	            }
	            
	            if (marketCourse.getType() != null && marketCourse.getType() != 0) {
	            	predicates.add(builder.equal(root.get("type"), marketCourse.getType()));
	            }
	
	            //搜索内容
	            if (StringUtils.isNotEmpty(marketCourse.getName())) {
	                predicates.add(builder.like(root.get("course").get("name"), "%" + marketCourse.getName() + "%"));
	            }
            }
            predicates.add(builder.like(root.get("delFlag"), Role.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
            return builder.and(predicatesArray);

        }, pageRequest);

        return new Page<MarketCourse>(dictPage);
    }

	@Override
	public int getOne(Long uid, int type) {
		MarketCourse marketCourse = this.marketCourseRepository.getOne(uid,type);
		return marketCourse != null ? 1 : 0;
	}

	@Override
	public int getCountByCid(Long cid) {
		return this.marketCourseRepository.getOneByCid(cid);
	}

}